package Test;



import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.DecisionTableConfiguration;
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import CCancer.*;


/**
 * This is a class to launch a decision table.
 */
public class DecisionTableTest {

    /**
     * @param args
     */
    //@SuppressWarnings("static-access")
	/**
	 * @param args
	 */
	public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            // go !
            
            
            Symptoms s1= new Symptoms();
            //s1.addToSymptoms("newly HTN");
            //s1.addToSymptoms("HTN");
            //s1.addToSymptoms("IHD");
            s1.addToSymptoms("fall");
           // s1.addToSymptoms("dizziness");
         
            System.out.println("Symptoms: "+s1);
            
            
            
           //the case Historys of the patient
            Historys h1=new Historys();
           //h1.addToHistorys("left ventricular hypertrophy");
            h1.addToHistorys("chronic kidney disease");
            
            h1.addToHistorys("diabetesmellitus");
           // h1.addToHistorys("Home ambulatory monitoring");
            System.out.println("Case Historys: "+h1);
            System.out.println("Case Historys contains: "+h1.inHistorys("diabetes mellitus"));
            
            //medicines of the patient
            Medicines m1=new Medicines(false); //will the medicine be changed?
            //the content of the medicines 
            m1.addToMedicines("Nasi");
            //m1.isChange();
            System.out.println("medicine : " + m1);
            System.out.println("medicine change?: " + m1.change);           
            
            //the indicators of the patient
          
            
            Indicator I1=new Indicator("BP",125);
            ksession.insert(I1);
            
            Indicator I2=new Indicator("new systolic BP",147); 
            ksession.insert(I2);
            
            Indicator I3=new Indicator("systolic BP1",146);
            ksession.insert(I3);
            
            Indicator I4=new Indicator("systolic BP2",147);
            ksession.insert(I4);
            
            Indicator I5=new Indicator("diastolic BP",130);
            ksession.insert(I5);
          
           
                      
            ksession.insert(s1);
            ksession.insert(h1);
            ksession.insert(m1);
           
          
            
           
           
           
            
        
            ksession.fireAllRules();
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
        config.setInputType(DecisionTableInputType.XLS);
        
        //to read the decinsion table files
        //kbuilder.add(ResourceFactory.newClassPathResource("withHTN.xls"), ResourceType.DTABLE, config);
        kbuilder.add(ResourceFactory.newClassPathResource("withoutHTN.xls"), ResourceType.DTABLE, config);
        kbuilder.add(ResourceFactory.newClassPathResource("GoalBPconfirm.xls"), ResourceType.DTABLE, config);
        
        
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

 }


